package com.linkhub.oms.admin.modules.stock.domain.dto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.linkhub.oms.admin.modules.wms.domain.dto.ProcessOrderNoticeDTO;
import com.linkhub.oms.admin.modules.wms.domain.dto.StockCountNoticeDTO;
import com.linkhub.oms.admin.modules.wms.domain.dto.WarehouseArrantConfirmDTO;
import com.linkhub.oms.common.util.WmsUtil;
import com.linkhub.oms.entity.ProductStockEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName ConfirmInboundDTO
 * @Description 确认入库的数据
 * @Author XieYu
 * @Date 2024/6/29 17:24
 * @Version 1.0
 */
@Data
public class ConfirmInboundDTO implements Serializable {
    private static final String DEFAULT_MANUFACTURE_NO = "0000";

    @ApiModelProperty(value = "用户id")
    private String userId;

    @ApiModelProperty(value = "供应商id")
    private String supplierId;

    @ApiModelProperty(value = "生产批号")
    private String manufactureNo;

    @ApiModelProperty(value = "仓库")
    private String stashName;

    @ApiModelProperty(value = "业务类型")
    private String businessType;

    @ApiModelProperty(value = "wms对应单号")
    private String wmsNo;

    @ApiModelProperty(value = "入库单号")
    private String inboundNo;

    @ApiModelProperty(value = "入库时间")
    private String inboundDate;

    @ApiModelProperty(value = "入库类型， 1：部分入库；0：全部入库")
    private String confirmType;

    @ApiModelProperty(value = "本地商品skuId")
    private String skuId;

    @ApiModelProperty(value = "类型, ZP：正品，CC：次品")
    private String type;

    @ApiModelProperty(value = "数量")
    private Integer skuQuantity;


    public static List<ConfirmInboundDTO> convert(WarehouseArrantConfirmDTO warehouseArrantConfirmDTO, String supplierId) {
        if (Objects.isNull(warehouseArrantConfirmDTO)) {
            return null;
        }
        List<ConfirmInboundDTO> results = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(warehouseArrantConfirmDTO.getProduct())) {
            warehouseArrantConfirmDTO.getProduct().forEach(p -> {
                if (CollectionUtils.isNotEmpty(p.getBatch())) {
                    p.getBatch().forEach(batch -> {
                        ConfirmInboundDTO confirmInboundDTO = new ConfirmInboundDTO();
                        confirmInboundDTO.setUserId(WmsUtil.getUserId(p.getItemCode()));
                        confirmInboundDTO.setSupplierId(supplierId);
                        confirmInboundDTO.setManufactureNo(StringUtils.defaultString(null, DEFAULT_MANUFACTURE_NO));
                        confirmInboundDTO.setStashName(warehouseArrantConfirmDTO.getWarehouseCode());
                        confirmInboundDTO.setWmsNo(warehouseArrantConfirmDTO.getEntryOrderId());
                        confirmInboundDTO.setInboundNo(warehouseArrantConfirmDTO.getEntryOrderCode());
                        confirmInboundDTO.setConfirmType(warehouseArrantConfirmDTO.getConfirmType());
                        confirmInboundDTO.setSkuId(WmsUtil.getSkuId(p.getItemCode()));
                        confirmInboundDTO.setType(batch.getInventoryType());
                        confirmInboundDTO.setSkuQuantity((int) Float.parseFloat(batch.getActualQty()));
                        confirmInboundDTO.setInboundDate(StringUtils.defaultIfBlank(batch.getStockinDate(), DateUtil.today()));
                        results.add(confirmInboundDTO);
                    });
                }
            });
        }
        return results;
    }

    public static List<ConfirmInboundDTO> convert(ProcessOrderNoticeDTO processOrderNoticeDTO) {
        List<ConfirmInboundDTO> results = Lists.newArrayList();
        if (Objects.nonNull(processOrderNoticeDTO) && CollUtil.isNotEmpty(processOrderNoticeDTO.getProductitems())) {
            for (ProcessOrderNoticeDTO.ProductItem productItem : processOrderNoticeDTO.getProductitems()) {
                ConfirmInboundDTO confirmInboundDTO = new ConfirmInboundDTO();
                confirmInboundDTO.setUserId(WmsUtil.getUserId(productItem.getItemCode()));
                confirmInboundDTO.setSupplierId("0");
                confirmInboundDTO.setManufactureNo(StringUtils.defaultString(null, DEFAULT_MANUFACTURE_NO));
                confirmInboundDTO.setStashName(processOrderNoticeDTO.getWarehouseCode());
                confirmInboundDTO.setInboundNo(processOrderNoticeDTO.getProcessOrderCode());
                confirmInboundDTO.setSkuId(WmsUtil.getSkuId(productItem.getItemCode()));
                confirmInboundDTO.setType(productItem.getInventoryType());
                confirmInboundDTO.setSkuQuantity(productItem.getQuantity());
                confirmInboundDTO.setInboundDate(DateUtil.formatDate(new Date()));
                results.add(confirmInboundDTO);
            }
        }

        return results;
    }

}
